import java.lang.reflect.Field;

public class Test7 {
    public static void main(String[] args) throws NoSuchFieldException {
        //通过反射加载一个Person实例
        try {
            Class clz = Class.forName("Person");
            Object person=clz.newInstance();
            //获取private String name属性
            Field name = clz.getDeclaredField("name");
            //name属性为private，这里已超出其访问方位，不能直接访问
            //通过setAccessable方法,设定为可以访问
            name.setAccessible(true);
            System.out.println("赋值前的name："+name.get(person));
            //为name属性赋值
            name.set(person,"New Person");
            System.out.println("赋值后的name："+name.get(person));
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
}
